package ro.peoplecentric.ctc.common.validator;

import java.util.Iterator;
import java.util.Map;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.dialect.MySQLDialect;

/**
 * This class overrides the same method from Dialect It is the fix for Criteria 
 * does not have a lock mode https://hibernate.atlassian.net/browse/HHH-5275
 *
 */
public class CustomMySQLDialect extends MySQLDialect {

    @Override
    public String getForUpdateString(String aliases, LockOptions lockOptions) {
        LockMode lockMode = lockOptions.getLockMode();
        Iterator<Map.Entry<String, LockMode>> itr = lockOptions.getAliasLockIterator();
        while (itr.hasNext()) {
            // seek for the highest lock mode
            final Map.Entry<String, LockMode> entry = itr.next();
            final LockMode lm = entry.getValue();
            if (lm.greaterThan(lockMode)) {
                lockMode = lm;
            }
        }
        lockOptions.setLockMode(lockMode);
        return getForUpdateString(lockOptions);
    }
}